import requests
import logging


logger = logging.getLogger("requests.session")
logging.basicConfig(level=logging.INFO,filename='requests.log',filemode='w',encoding='utf-8')
# 继承了request的Session
class Session(requests.Session):
    def __init__(self,base_url=""):
        self.base_url = base_url
        super().__init__()

    def request(
            self,
            method,
            url,
            *args,
            **kwargs
    ):
        if not url.startswith("http"):
            url = self.base_url + url
        return super().request(method, url, *args, **kwargs)

    def send(self, request: requests.PreparedRequest, *args, **kwargs):
        logger.info(f"发送请求---> 接口地址={request.method} {request.url}")
        logger.info(f"发送请求---> 请求头={request.headers}")
        logger.info(f"发送请求---> 请求体={request.body}")

        response = super().send(request, **kwargs)
        logger.info(f"接收响应 <---状态码={response.status_code}")
        logger.info(f"接收响应 <---响应头={response.headers}")
        logger.info(f"接收响应 <---响应体={response.text}")
        return response

if __name__ == '__main__':
    session = Session('https://www.baidu.com')
    resp1 = session.request('get', '')
    resp2 = session.request('get', '')


    # def request(
    #         self,
    #         method,
    #         url,
    #         params=...,
    #         data=...,
    #         headers=...,
    #         cookies=...,
    #         files=...,
    #         auth=...,
    #         timeout=...,
    #         allow_redirects=...,
    #         proxies=...,
    #         hooks=...,
    #         stream=...,
    #         verify=...,
    #         cert=...,
    #         json=...,
    # ):
